<template>
  <div class="auth-wrapper">
    <el-dialog
      title="权限设置"
      :close-on-click-modal="false"
      v-dialogDrag
      width="700px"
      :visible.sync="visible">
      <el-form class="auth" :model="inputForm" status-icon v-loading="loading" ref="inputForm" @keyup.enter.native="inputFormSubmit()"
               @submit.native.prevent>
        <el-row :gutter="30">
          <el-col :span="12">
            <div class="auth-tree">
              <span slot="label">查看数据权限</span>
              <el-scrollbar style="height: 450px">
                <el-tree
                  :data="dataRuleList"
                  :props=" {
                          label: 'name',
                          children: 'children'
                        }"
                  node-key="id"
                  ref="dataRuleTree"
                  :default-expanded-keys="['1']"
                  :default-checked-keys="dataRuleCheckedKeys"
                  show-checkbox>
                </el-tree>
              </el-scrollbar>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="auth-tree">
              <span slot="label">操作数据权限</span>
              <el-scrollbar style="height: 450px">
                <el-tree
                  :data="dataRuleList"
                  :props=" {
                          label: 'name',
                          children: 'children'
                        }"
                  node-key="id"
                  ref="operDataRuleTree"
                  :default-expanded-keys="['1']"
                  :default-checked-keys="operDataRuleCheckedKeys"
                  show-checkbox>
                </el-tree>
              </el-scrollbar>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" plain @click="inputFormSubmit()" v-noMoreClick>确定</el-button>
        <el-button @click="visible = false">取消</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        loading: false,
        title: '',
        dataRuleList: [],
        operDataRuleList: [],
        dataRuleCheckedKeys: [],
        operDataRuleCheckedKeys: [],
        inputForm: {
          id: '',
          dataRuleIds: '',
          operDataRuleIds: '',
          oldName: ''
        }
      }
    },
    methods: {
    	handleClose(done) {
			this.$Msg.confirm({
				title:'您确定要关闭吗？',
				content:this.GLOBAL.FORM_CLOSE_TIPS,
				confirmCall:()=>{
					done();
				}
			})
		},
      init (id) {
        this.inputForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.$refs.dataRuleTree.setCheckedKeys([])
          this.$refs.operDataRuleTree.setCheckedKeys([])
          let p1 = this.$http({
            url: `/sys/dataRule/treeData`,
            method: 'get'
          })
          let p2 = p1
          if (this.inputForm.id) {
            this.loading = true
            let p3 = this.$http({
              url: `/sys/user/queryById?id=${this.inputForm.id}`,
              method: 'get'
            })
            Promise.all([p1, p2, p3]).then((result) => {
              this.dataRuleList = result[0].data.treeData
              this.operDataRuleList = result[1].data.treeData
              let data = result[2].data
              if (data && data.success) {
                this.loading = false
                this.inputForm.id = data.result.id
                this.$refs.dataRuleTree.setCheckedKeys(data.result.dataRuleIds.split(','))
                this.$refs.operDataRuleTree.setCheckedKeys(data.result.operDataRuleIds.split(','))
                this.loading = false
              } else {
                this.loading = false
                this.visible = false
              }
            })
          } else {
            Promise.all([p1, p2]).then((result) => {
              this.dataRuleList = result[0].data.treeData
              this.operDataRuleList = result[1].data.treeData
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      inputFormSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          this.inputForm.dataRuleIds = this.$refs.dataRuleTree.getCheckedKeys().concat(this.$refs.dataRuleTree.getHalfCheckedKeys()).join(',')
          this.inputForm.operDataRuleIds = this.$refs.operDataRuleTree.getCheckedKeys().concat(this.$refs.operDataRuleTree.getHalfCheckedKeys()).join(',')
          if (valid) {
            this.loading = true
            this.$http({
              url: `/sys/user/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.$message.success(data.msg)
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .auth-wrapper{
    ::v-deep.el-dialog{
      .el-dialog__body{
        .el-scrollbar{
          margin-top: 5px;
        }
        .el-scrollbar__wrap{
          overflow: auto;
        }
        .auth-tree{
          background: #fff;
          border: 1px solid #dcdfe6;
          -webkit-box-shadow: 0 2px 4px 0 rgba(0,0,0,.12), 0 0 6px 0 rgba(0,0,0,.04);
          box-shadow: 0 2px 4px 0 rgba(0,0,0,.12), 0 0 6px 0 rgba(0,0,0,.04);
          padding: 20px;
        }
      }
    }
  }

  .el-tree {
    min-width: 100%;
    display: inline-block !important;
  }

  .el-scrollbar {
    height: 100%;
  }
</style>
